﻿using Item.Buff;
using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Player.Fort;

namespace ItemComponent
{
    public class Army : MyItemComponentMono
    {
        protected const string Key_ArmyData = "ArmyData";

#if UNITY_EDITOR
        [ReadOnly, SerializeField]
        private STArmyData debugArmyData;
#endif

        protected FortHP HP => Player.MyPlayer.Ins.MyFort.HP;

        public STArmyData ArmyData { get; protected set; }
        public STArmyData CurArmyData { get; protected set; }

        private void Awake()
        {
            enabled = false;
        }

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            if (data == null | data[Key_ArmyData] == null) return;
            InitArmy(JsonMapper.ToObject<STArmyData>(data[Key_ArmyData].ToJson()));
        }

        protected void InitArmy(STArmyData armyData)
        {
#if UNITY_EDITOR
            debugArmyData = armyData;
#endif

            this.ArmyData= armyData;
            this.CurArmyData = armyData;
        }

        private void OnEnable()
        {
            OnAddArmy();
        }

        private void OnDisable()
        {
            OnRemoveArmy();
        }

        private void OnAddArmy()
        {
            var tmpHP = HP;

            if (CurArmyData.nMaxHpNum != 0)
                tmpHP.MaxHp += CurArmyData.nMaxHpNum;

            if (CurArmyData.dShild_Fixed != 0)
                tmpHP.Shild_Fixed += (float)CurArmyData.dShild_Fixed;
            if (CurArmyData.dShild_Percent != 0)
                tmpHP.AddShild_Percent((float)CurArmyData.dShild_Percent);

            if (CurArmyData.dHPS_Fixed != 0)
                tmpHP.HPS_Fixed += (float)CurArmyData.dHPS_Fixed;
            if (CurArmyData.dHPS_Percent != 0)
                tmpHP.HPS_Percent += (float)CurArmyData.dHPS_Percent;
        }
        private void OnRemoveArmy()
        {
            var tmpHP = HP;

            if (CurArmyData.nMaxHpNum != 0)
                tmpHP.MaxHp -= CurArmyData.nMaxHpNum;

            if (CurArmyData.dShild_Fixed != 0)
                tmpHP.Shild_Fixed -= (float)CurArmyData.dShild_Fixed;
            if (CurArmyData.dShild_Percent != 0)
                tmpHP.AddShild_Percent(-(float)CurArmyData.dShild_Percent);

            if (CurArmyData.dHPS_Fixed != 0)
                tmpHP.HPS_Fixed -= (float)CurArmyData.dHPS_Fixed;
            if (CurArmyData.dHPS_Percent != 0)
                tmpHP.HPS_Percent -= (float)CurArmyData.dHPS_Percent;
        }

        public override void OnBuffChange(STBuffEffectData buffEffectData)
        {
            OnRemoveArmy();

            STArmyData tmpData = ArmyData;

            tmpData.nMaxHpNum = (int)(tmpData.nMaxHpNum * buffEffectData.fPower);

            tmpData.dShild_Fixed *= buffEffectData.fPower;
            tmpData.dShild_Percent *= buffEffectData.fPower;

            tmpData.dHPS_Fixed *= buffEffectData.fSpeed;
            tmpData.dHPS_Percent *= buffEffectData.fSpeed;

            CurArmyData = tmpData;

#if UNITY_EDITOR
            debugArmyData = CurArmyData;
#endif
            OnAddArmy();
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(STArmyData armyData)
        {
            JsonData json = new JsonData();
            json[Key_ArmyData] = JsonMapper.ToObject(JsonMapper.ToJson(armyData));
            return new STMyComponetData()
            {
                strType = typeof(Army).FullName,
                data = json,
            };
        }
#endif
    }
}
